#include "homePage.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDesktopServices>
#include <ElaMenu.h>
#include <ElaText.h>
#include <ElaTheme.h>
#include <ElaToolButton.h>
#include <ElaImageCard.h>
#include <ElaAcrylicUrlCard.h>
#include <ElaToolTip.h>
#include <ElaScrollArea.h>
#include <ElaPopularCard.h>
#include <ElaFlowLayout.h>

HomePage::HomePage(QWidget *parent)
    : ElaScrollPage(parent)
{
    setWindowTitle("Home");
    setTitleVisible(false);
    setContentsMargins(2, 2, 0, 0);

    ElaText* desText = new ElaText("FluentUI For Ms. Liu", this);
    desText->setTextPixelSize(35);

    QVBoxLayout* titleLayout = new QVBoxLayout();
    titleLayout->setContentsMargins(30, 60, 0, 0);
    titleLayout->addWidget(desText);

    ElaImageCard* backgroundCard = new ElaImageCard(this);
    backgroundCard->setBorderRadius(10);
    backgroundCard->setFixedHeight(400);
    backgroundCard->setMaximumAspectRatio(1.7);
    backgroundCard->setCardImage(QImage(":/Image/Home_Background.png"));

    ElaAcrylicUrlCard* urlCard1 = new ElaAcrylicUrlCard(this);
    urlCard1->setCardPixmapSize(QSize(62, 62));
    urlCard1->setFixedSize(195, 225);
    urlCard1->setTitlePixelSize(17);
    urlCard1->setTitleSpacing(25);
    urlCard1->setSubTitleSpacing(13);
    urlCard1->setUrl("https://gitee.com/wang917/liu-da-xia");
    urlCard1->setCardPixmap(QPixmap(":/Image/github.png"));
    urlCard1->setTitle("Github Link");
    urlCard1->setSubTitle("Software that belongs only to Liu CuiXia");
    ElaToolTip* urlCard1ToolTip = new ElaToolTip(urlCard1);
    urlCard1ToolTip->setToolTip("https://gitee.com/wang917/liu-da-xia");

    ElaScrollArea* cardScrollArea = new ElaScrollArea(this);
    cardScrollArea->setWidgetResizable(true);
    cardScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    cardScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    cardScrollArea->setIsGrabGesture(true, 0);
    cardScrollArea->setIsOverShoot(Qt::Horizontal, true);
    QWidget* cardScrollAreaWidget = new QWidget(this);
    cardScrollAreaWidget->setStyleSheet("background-color:transparent;");
    cardScrollArea->setWidget(cardScrollAreaWidget);
    QHBoxLayout* urlCardLayout = new QHBoxLayout();
    urlCardLayout->setSpacing(15);
    urlCardLayout->setContentsMargins(30, 0, 0, 6);
    urlCardLayout->addWidget(urlCard1);
    urlCardLayout->addStretch();
    QVBoxLayout* cardScrollAreaWidgetLayout = new QVBoxLayout(cardScrollAreaWidget);
    cardScrollAreaWidgetLayout->setContentsMargins(0, 0, 0, 0);
    cardScrollAreaWidgetLayout->addStretch();
    cardScrollAreaWidgetLayout->addLayout(urlCardLayout);

    QVBoxLayout* backgroundLayout = new QVBoxLayout(backgroundCard);
    backgroundLayout->setContentsMargins(0, 0, 0, 0);
    backgroundLayout->addLayout(titleLayout);
    backgroundLayout->addWidget(cardScrollArea);

    ElaText* flowText = new ElaText("工具列表", this);
    flowText->setTextPixelSize(20);
    QHBoxLayout* flowTextLayout = new QHBoxLayout();
    flowTextLayout->setContentsMargins(33, 0, 0, 0);
    flowTextLayout->addWidget(flowText);

    ElaPopularCard* musicCard = new ElaPopularCard(this);
    connect(musicCard, &ElaPopularCard::popularCardButtonClicked, this, [=]() { Q_EMIT musicCardNavigation(); });
    musicCard->setTitle("MusicTool");
    musicCard->setSubTitle("5.0⭐ 快速获取歌词程序");
    musicCard->setCardPixmap(QPixmap(":/Image/music.png"));
    musicCard->setInteractiveTips("免费使用");
    musicCard->setDetailedText("使用MusicTool通过歌单连接快速获取歌曲信息，目前支持qq音乐、网易云音乐和酷狗音乐。");
    musicCard->setCardFloatPixmap(QPixmap(":/Image/IARC/IARC_7+.svg.png"));

    // ElaPopularCard* magicCard = new ElaPopularCard(this);
    // connect(magicCard, &ElaPopularCard::popularCardButtonClicked, this, [=]() { Q_EMIT magicCarNavigation(); });
    // magicCard->setTitle("MagicTool");
    // magicCard->setSubTitle("5.0⭐ 神秘应用");
    // magicCard->setCardPixmap(QPixmap(":/Image/magic.png"));
    // magicCard->setInteractiveTips("免费使用");
    // magicCard->setDetailedText("暂时不透露任何信息，敬请期待！🌷🌷🌷");

    ElaFlowLayout* flowLayout = new ElaFlowLayout(0, 5, 5);
    flowLayout->setContentsMargins(30, 0, 0, 0);
    flowLayout->setIsAnimation(true);
    flowLayout->addWidget(musicCard);
    // flowLayout->addWidget(magicCard);

    QWidget* centralWidget = new QWidget(this);
    centralWidget->setWindowTitle("Home");
    QVBoxLayout* centerVLayout = new QVBoxLayout(centralWidget);
    centerVLayout->setSpacing(0);
    centerVLayout->setContentsMargins(0, 0, 0, 0);
    centerVLayout->addWidget(backgroundCard);
    centerVLayout->addSpacing(20);
    centerVLayout->addLayout(flowTextLayout);
    centerVLayout->addSpacing(10);
    centerVLayout->addLayout(flowLayout);
    centerVLayout->addStretch();
    addCentralWidget(centralWidget);

    connect(eTheme, &ElaTheme::themeModeChanged, this, [=]() {
        if (!parent)
        {
            update();
        }
    });
}
